﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Volunteering.model;
using Volunteering.service;

namespace Volunteering.ui {
  public partial class MainWind : Form {
    private readonly SpecialityService service;

    public MainWind(SpecialityService service) {
      InitializeComponent();
      this.service = service;
    }

    private const string TXT_SEARCHING = "正在查询，请稍候...";
    private const string TXT_DONE = "共查询到{0}个专业。";
    private const string TXT_READY_FOR_KEYS = "多个关键字需以逗号或空格分隔，以空白进行查询将返回所有专业。";
    private const string TXT_READY_FOR_SUBJECTS = "主科必须二选一，辅科必须四选二，方可点击按钮进行查询。";
    private const string TXT_HELP = "3+1+2高考模式政策简要说明";

    private void textKeys_TextChanged(object sender, EventArgs e) {
      labelStatus.Text = TXT_READY_FOR_KEYS;
      gridView.Rows.Clear();
    }

    private void textKeys_KeyPress(object sender, KeyPressEventArgs e) {
      if (e.KeyChar.Equals(Keys.Enter))
        buttonSearchKeys_Click(sender, e);
    }

    private void buttonSearchKeys_Click(object sender, EventArgs e) {
      labelStatus.Text = TXT_SEARCHING;
      gridView.Rows.Clear();

      var result = service.OnCommand(new SearchByKeys(textKeys.Text));
      result.ForEach(r => gridView.Rows.Add(r.Name, r.Discipline, r.Major,
        string.Join(", ", r.MainSubjects.Select(s => s.ToString())),
        string.Join(", ", r.ExtSubjects.Select(s => (s.Equals(r.EssentialSubject)) ? "*" + s.ToString() : s.ToString()))
        ));

      labelStatus.Text = string.Format(TXT_DONE, result.Count);
    }

    private void tabControlSearch_SelectedIndexChanged(object sender, EventArgs e) {
      gridView.Rows.Clear();

      var page = tabControlSearch.SelectedTab;     

      if (page.Equals(tabPageKeys)) {        
        labelStatus.Text = TXT_READY_FOR_KEYS;
        textKeys.Text = "";

        this.AcceptButton = buttonSearchKeys;

        textPolicy.Hide();
        gridView.Show();
      } else if (page.Equals(tabPageSubjects)) {        
        labelStatus.Text = TXT_READY_FOR_SUBJECTS;

        radioPhysics.Checked = false;
        radioHistory.Checked = false;
        selectedMain = null;

        checkPolitics.Checked = false;
        checkGeography.Checked = false;
        checkChemistry.Checked = false;
        checkBiology.Checked = false;
        selectedExt.Clear();

        buttonSearchSubjects.Enabled = false;
        this.AcceptButton = buttonSearchSubjects;

        textPolicy.Hide();
        gridView.Show();
      } else {
        labelStatus.Text = TXT_HELP;

        textPolicy.Show();
        gridView.Hide();        
      }
    }

    private Subjects? selectedMain = null;
    private HashSet<Subjects> selectedExt = new HashSet<Subjects>();

    private void EnableSearchButton() {
      labelStatus.Text = TXT_READY_FOR_SUBJECTS;
      gridView.Rows.Clear();

      buttonSearchSubjects.Enabled = (selectedMain.HasValue) && (selectedExt.Count == 2);
    }

    private void radioPhysics_CheckedChanged(object sender, EventArgs e) {
      if (radioPhysics.Checked)
        selectedMain = Subjects.物理;

      EnableSearchButton();
    }

    private void radioHistory_CheckedChanged(object sender, EventArgs e) {
      if (radioHistory.Checked)
        selectedMain = Subjects.历史;

      EnableSearchButton();
    }

    private void checkPolitics_CheckedChanged(object sender, EventArgs e) {
      if (checkPolitics.Checked)
        selectedExt.Add(Subjects.政治);
      else
        selectedExt.Remove(Subjects.政治);

      EnableSearchButton();
    }

    private void checkGeography_CheckedChanged(object sender, EventArgs e) {
      if (checkGeography.Checked)
        selectedExt.Add(Subjects.地理);
      else
        selectedExt.Remove(Subjects.地理);

      EnableSearchButton();
    }

    private void checkChemistry_CheckedChanged(object sender, EventArgs e) {
      if (checkChemistry.Checked)
        selectedExt.Add(Subjects.化学);
      else
        selectedExt.Remove(Subjects.化学);

      EnableSearchButton();
    }

    private void checkBiology_CheckedChanged(object sender, EventArgs e) {
      if (checkBiology.Checked)
        selectedExt.Add(Subjects.生物);
      else
        selectedExt.Remove(Subjects.生物);

      EnableSearchButton();
    }

    private void buttonSearchSubjects_Click(object sender, EventArgs e) {
      labelStatus.Text = TXT_SEARCHING;
      gridView.Rows.Clear();

      var result = service.OnCommand(new SearchBySubjects(selectedMain.Value, selectedExt));
      result.ForEach(r => gridView.Rows.Add(r.Name, r.Discipline, r.Major,
        string.Join(", ", r.MainSubjects.Select(s => s.ToString())),
        string.Join(", ", r.ExtSubjects.Select(s => (s.Equals(r.EssentialSubject)) ? "*" + s.ToString() : s.ToString()))
        ));

      labelStatus.Text = string.Format(TXT_DONE, result.Count);
    }
  }
}
